/**
 * Created by Baggio on 2019-1-8.
 */
$(function () {
    $(".sub_button").click(function () {
        $(".purchase_details").show();
        $(".background_box").show();
    });
    $(".purchase_details .top .esc").click(function () {
        $(this).parents(".purchase_details").hide();
        $(this).parents(".purchase_details").siblings(".background_box").hide();
    });
    $(".background_box").click(function () {
        $(this).hide();
        $(this).siblings(".purchase_details").hide();
    })
    $(".purchase_details .popup button").click(function () {
        $(".purchase_details .popup").hide();
    })
    /**
     *  购物车
     */
    $(".button_box .Shopping_Cart").click(function () {

        var productEntityId = $("#productEntityId").val();
        var amount = $(".amount_box").find("input[type=text]").val();
        var postData = {productEntityId: productEntityId, amount: amount}
        $.ajax({
            type: "post",
            url: "/shoppingCart/addShoppingCart",
            dataType: "json",
            async: false,
            data: postData,
            success: function (res) {
                console.log(res);
                $(".purchase_details .popup").find("h5").html("成功加入购物车");
                if (res.code != 1) {
                    //  alert(res.msg);
                    $(".purchase_details .popup").find("h5").html(res.msg);
//                    return;
                }

                $(".purchase_details .popup").show();
            }

        });


    })
    /**
     * 立即购买
     */
    $(".button_box .determine").click(function () {
        var productEntityId = $("#productEntityId").val();
        var amount = $(".amount_box").find("input[type=text]").val();
        var productJsons = [];
        var productJson = {productEntityId: productEntityId, amount: amount};
        productJsons.push(productJson);

        // var consigneeName = $(".contact_box").find("input[name=userName]").val();
        // var consigneePhone = $(".contact_box").find("input[name=phone]").val();
        // var consigneeAddress = $(".contact_box").find(".address").html();
        // if (!isNull(consigneePhone)) {
        //     $(".purchase_details .popup").find("h5").html("联系电话不能为空！");
        //     $(".purchase_details .popup").show();
        //     return;
        // }
        // if (!checkPhone(consigneePhone)) {
        //     $(".purchase_details .popup").find("h5").html("联系电话格式不对！");
        //     $(".purchase_details .popup").show();
        //     return;
        // }
        // if (!isNull(consigneeName)) {
        //     $(".purchase_details .popup").find("h5").html("联系人不能为空！");
        //     $(".purchase_details .popup").show();
        //     return;
        // }
        // if (!isNull(consigneeAddress)) {
        //     $(".purchase_details .popup").find("h5").html("联系地址不能为空！");
        //     $(".purchase_details .popup").show();
        //     return;
        // }

        var postData = {
          productJsons: JSON.stringify(productJsons)  // ,
            // consigneeName: consigneeName,
            // consigneePhone: consigneePhone,
            // consigneeAddress: consigneeAddress
        };
        $.ajax({
            type: "post",
            url: "/order/createOrder",
            dataType: "json",
            async: false,
            data: postData,
            success: function (res) {
                console.log(res);
                if (res.code != 1) {
                    alert(res.msg);
                    return;
                }
                var orderId = res.data.orderId;
                if (orderId == null || orderId == undefined || orderId == '') {
                    alert("订单生成失败！");
                }
                window.location.href = "/pay/" + orderId;
            }

        });
    })

});
